package ee.kuli.hk.rent;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DeviceListAdapter extends ArrayAdapter<Device> {
	private final Activity activity;
	private final List<Device> devices;
	public ImageLoader loader;
	
	public DeviceListAdapter(Activity activity, List<Device> objects) {
		super(activity, R.layout.device_row, objects);
		this.activity = activity;
		this.devices = objects;
		this.loader = new ImageLoader(activity);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
        DeviceRowView sqView = null;
 
        if(rowView == null)
        {
            // Get a new instance of the row layout view
            LayoutInflater inflater = activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.device_row, null);
 
            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sqView = new DeviceRowView();
            sqView.model = (TextView) rowView.findViewById(R.id.device_listview_model);
            sqView.description = (TextView) rowView.findViewById(R.id.device_listview_description);
            sqView.value = (TextView) rowView.findViewById(R.id.device_listview_value);
            sqView.serial = (TextView) rowView.findViewById(R.id.device_listview_serial);
           // sqView.imei = (TextView) rowView.findViewById(R.id.device_listview_imei);
            sqView.image = (ImageView) rowView.findViewById(R.id.device_listview_image);
 
            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sqView);
        } else {
            sqView = (DeviceRowView) rowView.getTag();
        }
 
        // Transfer the stock data from the data object
        // to the view objects
        Device currentDevice = devices.get(position);
        sqView.model.setText(currentDevice.getModel());
        sqView.description.setText(currentDevice.getDescription());
        sqView.value.setText(currentDevice.getValue());
        sqView.serial.setText(currentDevice.getSerial());
      //  sqView.imei.setText(currentDevice.getImei());
        loader.DisplayImage(currentDevice.getImageURL().toString(), sqView.image);

        return rowView;
	}

	protected static class DeviceRowView {
        protected TextView model;
        protected TextView description;
        protected TextView value;
        protected TextView serial;
       // protected TextView imei;
        protected ImageView image;
        protected String image_url = "";
    }
}
